package com.craigcmiller.cmtetris.core;


/**
 * Specific block type
 * @author craig
 */
public class ReverseLBlock extends Block
{
	private int rotation;
	
	
	/**
	 * Construct the block
	 * @param b
	 */
	public ReverseLBlock(Board b)
	{
		super(b);
		
		this.rotation=0;
	}
	
	
	/**
	 * Create the initial version of the block
	 */
	@Override
	protected void build()
	{
		cells.add(new BlockCell(board.getWidth()/2, 0));
		cells.add(new BlockCell(board.getWidth()/2, 1));
		cells.add(new BlockCell(board.getWidth()/2, 2));
		cells.add(new BlockCell(board.getWidth()/2-1, 2));
	}
	
	
	/**
	 * Rotate the block
	 */
	@Override
	public void rotate()
	{
		switch (rotation) {
			case 0:
				if (isCellMoveLegal(0, 1, 1) && isCellMoveLegal(2, -1, -1) && isCellMoveLegal(3, 0, -2)) {
					moveCell(0, 1, 1);
					moveCell(2, -1, -1);
					moveCell(3, 0, -2);
					
					rotation=1;
				}
				
				break;
			case 1:
				if (isCellMoveLegal(0, -1, 1) && isCellMoveLegal(2, 1, -1) && isCellMoveLegal(3, 2, 0)) {
					moveCell(0, -1, 1);
					moveCell(2, 1, -1);
					moveCell(3, 2, 0);
					
					rotation=2;
				}
				
				break;
			case 2:
				if (isCellMoveLegal(0, -1, -1) && isCellMoveLegal(2, 1, 1) && isCellMoveLegal(3, 0, 2)) {
					moveCell(0, -1, -1);
					moveCell(2, 1, 1);
					moveCell(3, 0, 2);
					
					rotation=3;
				}
				
				break;
			case 3:
				if (isCellMoveLegal(0, 1, -1) && isCellMoveLegal(2, -1, 1) && isCellMoveLegal(3, -2, 0)) {
					moveCell(0, 1, -1);
					moveCell(2, -1, 1);
					moveCell(3, -2, 0);
					
					rotation=0;
				}
				
				break;
		}
	}
}
